#include "NormalSample.h"
#include "Application.h"
#include "utils.h"
#include "Utility.h"
#include <thread>
namespace RayTracer
{
	void NormalSamples::Init()
	{
		curIndex = 0;
		isGenerate = false;
		mImageParam = ImageParam(800, 500, "Normal.png");
		viewport_height = 2.0;
		viewport_width = aspect_ratio * viewport_height;
		focal_length = 1.0;

		horizontal = vec3(viewport_width, 0, 0);
		vertical = vec3(0, viewport_height, 0);
		lower_left_corner = origin - horizontal / 2.0f - vertical / 2.0f - vec3(0, 0, focal_length);

		upColor = vec3(0.5, 0.7, 1.0);
		downColor = vec3(1.0);
		sphereColor = vec3(1.0, 0.0, 0.0);
		sphere1 = std::make_shared<Sphere>(vec3(0, 0, -1), 0.5);
		sphere2 = std::make_shared<Sphere>(vec3(0, -100.5, -1), 100);
		world.add(sphere1);
		world.add(sphere2);
	}

	void NormalSamples::Render()
	{
		int width = mImageParam.mWidth;
		int height = mImageParam.mHeight;
		uint8_t* pixels = new uint8_t[width * height * 3];
		int index = 0;
		for (int j = height - 1; j >= 0; j--) {
			for (int i = 0; i < width; ++i) {
				auto u = float(i) / (width - 1);
				auto v = float(j) / (height - 1);
				Ray ray(origin, lower_left_corner + u * horizontal + v * vertical - origin);
				vec3 color = RayColor(ray,world);

				pixels[index++] = static_cast<uint8_t>(255.999f * color.r);
				pixels[index++] = static_cast<uint8_t>(255.999f * color.g);
				pixels[index++] = static_cast<uint8_t>(255.999f * color.b);
				curIndex++;
			}
		}
		Utils::SaveImage(("Images/" + mImageParam.mName).c_str(), width, height, 3, pixels);
		isGenerate = true;
	}

	void NormalSamples::ImGui()
	{
		ImGui::Text(u8"Scene Normal");
		ImGui::Checkbox(u8"Normal",&isNormal);
		ImGui::Separator();
		ImGui::Text(u8"Sky");
		ImGui::ColorEdit3(u8"High", &upColor[0]);
		ImGui::ColorEdit3(u8"Low", &downColor[0]);
		ImGui::Separator();
		// Sphere 1
		ImGui::Text(u8"Sphere1");
		ImGui::InputFloat3(u8"Sphere1 Center",&sphere1->center[0]);
		ImGui::SliderFloat(u8"Sphere1 Radius",&sphere1->radius,0.0f,5.0f);
		ImGui::Separator();
		// Sphere 2
		ImGui::Text(u8"Sphere2");
		ImGui::InputFloat3(u8"Sphere2 Center", &sphere2->center[0]);
		ImGui::SliderFloat(u8"Sphere2 Radius", &sphere2->radius, 0.0f, 100.0f);
		ImGui::Separator();
		auto w = (float)mImageParam.mWidth;
		auto h = (float)mImageParam.mHeight;
		auto progress = ((float)curIndex / (w * h));
		ImGui::ProgressBar(progress);
		if (ImGui::Button(u8"Generate")) {
			std::thread t = std::thread([=]() { Render(); });
			t.detach();
		}
		if (isGenerate) {
			auto mTexHandle = Utils::LoadTexture(("Images/" + mImageParam.mName).c_str());
			Application::GetInstance()->SetTexHandle(mTexHandle);
			isGenerate = false;
			curIndex = 0;
		}
		ImGui::SameLine();
		if (ImGui::Button("Reset")) { Reset(); }

	}

	void NormalSamples::ShutDown()
	{
		Application::GetInstance()->SetTexHandle(0);
	}

	vec3 NormalSamples::RayColor(const Ray & r, const Hitable& world)
	{
		HitRecord rec;
		if (world.Hit(r, 0, infinity, rec)) 
		{
			vec3 color = 0.5f * (rec.normal + vec3(1, 1, 1));
			return isNormal ? color : sphereColor;
		}
		
		vec3 unit_direction = glm::normalize(r.direction());
		auto t = 0.5f*(unit_direction.y + 1.0f);
		return (1.0f - t)*downColor + t * upColor;
	}

	void NormalSamples::Reset()
	{
		curIndex = 0;
		isGenerate = false;
		upColor = vec3(0.5, 0.7, 1.0);
		downColor = vec3(1.0);
		sphereColor = vec3(1.0, 0.0, 0.0);
		Application::GetInstance()->SetTexHandle(0);
	}
}